package 链表;

/**
 * @author: 大怪
 * @email: 962527441@qq.com
 */

/**
 * 题目：
 * 给定一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
 *
 * 示例 1：
 * 输入：head = [1,2,3,4,5], n = 2
 * 输出：[1,2,3,5]
 */
public class 删除链表的倒数第n个结点 {

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode pre =  new ListNode(0);
            pre.next = head;
            ListNode tmp = pre;
            ListNode res =  pre;
            int p1 = 0;
            int p2 = 0;
            for (int i = 0; i < n; i++) {
                tmp = tmp.next;
            }
            while(tmp.next != null) {
                tmp = tmp.next;
                res = res.next;
            }
            res.next = res.next.next;
            return pre.next;
        }
    }


}
